﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Threading;

namespace AlgoLogicMessenger
{
    //Interaction logic for MainWindow.xaml
    public partial class MainWindow : Window
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);

        public MainWindow()
        {
            InitializeComponent();
            LogIn login = new LogIn();
            frame1.NavigationService.Navigate(login);
            SplashDemo.Splasher.CloseSplash();
        }

        private void BtnExit_Click(object sender, RoutedEventArgs e)
        {
            if (IP.Address != null)
            {
                if (clientSocket.Connected != true)
                {
                    clientSocket.Connect(IP.Address, 8888);
                    serverStream = clientSocket.GetStream();
                    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("" + "#");
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }
                if (MessageBox.Show("Are You Sure To Log Out All Programs?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    byte[] outStream1 = System.Text.Encoding.ASCII.GetBytes("@ForceAllLogOut|#");
                    if (serverStream != null)
                    {
                        serverStream.Write(outStream1, 0, outStream1.Length);
                        serverStream.Flush();
                    }
                    Environment.Exit(0);          
                }
            }
            else
            {
                MessageBox.Show("You Are Not Connected To The Server !");
            }
        }

        private void BtnMini_Click(object sender, RoutedEventArgs e)
        {
            Window w = Application.Current.MainWindow;
            w.Activate();

            if (w.WindowState == WindowState.Normal)
            {
                w.WindowState = WindowState.Minimized;
            }
        }
    }
}